package com.bawei.module_mine.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_image.GlideApp
import com.bawei.lib_storage.database.FavoriteManager
import com.bawei.lib_storage.database.entity.FavoriteVideoEntity
import com.bawei.module_mine.R
import com.bawei.module_mine.databinding.ItemCollectVideoBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.*

class CollectVideoAdapter(
    private val coroutineScope: CoroutineScope,
    private val userId: String = "default_user",
    private val onItemClick: ((FavoriteVideoEntity) -> Unit)? = null,
    private val onUnfavorite: ((FavoriteVideoEntity, Int) -> Unit)? = null
) : RecyclerView.Adapter<CollectVideoAdapter.CollectVideoViewHolder>() {

    private val collectList = mutableListOf<FavoriteVideoEntity>()
    private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())

    fun setData(data: List<FavoriteVideoEntity>) {
        collectList.clear()
        collectList.addAll(data)
        notifyDataSetChanged()
    }

    fun removeItem(position: Int) {
        if (position in 0 until collectList.size) {
            collectList.removeAt(position)
            notifyItemRemoved(position)
        }
    }

    inner class CollectVideoViewHolder(private val binding: ItemCollectVideoBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(item: FavoriteVideoEntity, position: Int) {
            binding.apply {
                tvCollectVideoTitle.text = item.title
                tvCollectVideoDescription.text = item.description
                tvCollectVideoTime.text = "收藏时间: ${dateFormat.format(Date(item.collectTime))}"

                // 加载封面图片
                GlideApp.with(itemView.context)
                    .load(item.coverUrl)
                    .placeholder(R.color.gray)
                    .into(ivCollectVideoCover)

                // 设置取消收藏按钮点击事件
                ivCollectVideoUnfavorite.setOnClickListener {
                    handleUnfavoriteClick(item, position)
                }

                // 设置整个item的点击事件
                itemView.setOnClickListener {
                    onItemClick?.invoke(item)
                }
            }
        }

        private fun handleUnfavoriteClick(item: FavoriteVideoEntity, position: Int) {
            coroutineScope.launch {
                val success = FavoriteManager.removeFavorite(item.videoId, userId)
                if (success) {
                    withContext(Dispatchers.Main) {
                        onUnfavorite?.invoke(item, position)
                    }
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CollectVideoViewHolder {
        val binding = ItemCollectVideoBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return CollectVideoViewHolder(binding)
    }

    override fun onBindViewHolder(holder: CollectVideoViewHolder, position: Int) {
        holder.bind(collectList[position], position)
    }

    override fun getItemCount(): Int = collectList.size
} 